#include "cashcontext.h"

#include <iostream>

using namespace std;

int main()
{
    double total(0.0);
    string exit;
    do
    {
        try
        {
            double price(0.0);
            cout << "请输入商品单价：" << endl;
            cin >> price;
            cout << "请输入商品数量：" << endl;
            int count(0);
            cin >> count;
            if (price <= 0 || count <= 0)
            {
                cout << "商品单价或者商品数量不能小于或等于零" << endl;
                continue;
            }

            cout << "请选择以下数字，确认商品的销售模式：" << endl;
            cout << "1.原价 2.八折 3.七折 4.满300返100" << endl;
            int mode(0);
            cin >> mode;
            CashContext *cashContext = new CashContext(mode);
            double totalSomeGoods = cashContext->calculate(price, count);
            delete cashContext;
            cashContext = nullptr;
            total += totalSomeGoods;
            cout << "单价：" << price << "元 数量："
                 << count << " 合计：" << totalSomeGoods << "元" << endl;
            cout << "总计：" << total << "元" << endl;
            cout << "是否退出系统？(y/n)" << endl;
            cin >> exit;
        }
        catch (const exception &e)
        {
            cerr << e.what() << '\n';
        }
    } while (exit.compare("y") != 0);

    return 0;
}